const JavaWorker=(monaco)=>{
monaco.languages.register({
  id: 'java',
  extensions: ['.java'],
  aliases: ['Java', 'java'],
  mimetypes: ['text/x-java-source', 'text/x-java', 'text/java']
})
//java关键字配置高亮配置
monaco.languages.setMonarchTokensProvider('java', {
  defaultToken: '',
  tokenPostfix: '.java',
  keywords: [
    'abstract',
    'assert',
    'boolean',
    'break',
    'byte',
    'case',
    'catch',
    'char',
    'class',
    'const',
    'continue',
    'default',
    'do',
    'double',
    'else',
    'enum',
    'extends',
    'final',
    'finally',
    'float',
    'for',
    'goto',
    'if',
    'implements',
    'import',
    'instanceof',
    'int',
    'interface',
    'long',
    'native',
    'new',
    'package',
    'private',
    'protected',
    'public',
    'return',
    'short',
    'static',
    'strictfp',
    'super',
    'switch',
    'synchronized',
    'this',
    'throw',
    'throws',
    'transient',
    'try',
    'void',
    'volatile',
    'while'
  ],
  typeKeywords: [
    'int',
    'float',
    'boolean',
    'char',
    'void',
    'double',
    'byte',
    'long',
    'short'
  ],
  operators: [
    '=',
    '>',
    '<',
    '!',
    '~',
    '?',
    ':',
    '==',
    '<=',
    '>=',
    '!=',
    '&&',
    '||',
    '++',
    '--',
    '+',
    '-',
    '*',
    '/',
    '&',
    '|',
    '^',
    '%',
    '<<',
    '>>',
    '>>>',
    '+=',
    '-=',
    '*=',
    '/=',
    '&=',
    '|=',
    '^=',
    '%=',
    '<<=',
    '>>=',
    '>>>='
  ],
  symbols: /[=><!~?:&|+\-*\/^%]+/,

  // C-like comments
  comments: {
    lineComment: '//',
    blockComment: ['/*', '*/']
  },

  // Numbers
  digits: /\d+(_+\d+)*/,
  octaldigits: /[0-7]+(_+[0-7]+)*/,
  binarydigits: /[0-1]+(_+[0-1]+)*/,
  hexdigits: /[[0-9a-fA-F]+(_+[0-9a-fA-F]+)*/,
  tokenizer: {
    root: [
      // identifiers and keywords
      [
        /[a-zA-Z_]\w*/,
        {
          cases: {
            '@keywords': 'keyword',
            '@typeKeywords': 'type',
            '@default': 'identifier'
          }
        }
      ],

      // whitespace
      { include: '@whitespace' },
      // delimiters and operators
      [/[{}()\[\]]/, '@brackets'],
      [/[<>](?!@symbols)/, '@brackets'],
      [/@symbols/, { cases: { '@operators': 'operator', '@default': '' } }],
      // @ annotations
      [/@\s*[a-zA-Z_\$][\w\$]*/, 'annotation'],
      // numbers
      [/\d*\.\d+([eE][\-+]?\d+)?[fFdD]?/, 'number.float'],
      [/0[xX]@hexdigits[nN]?/, 'number.hex'],
      [/0[bB]@binarydigits[nN]?/, 'number.binary'],
      [/0[oO]?@octaldigits[nN]?/, 'number.octal'],
      [/0|[1-9]\d*[lL]?/, 'number'],
      // strings
      [/"([^"\\]|\\.)*$/, 'string.invalid'], // non-teminated string
      [/"/, { token: 'string.quote', next: '@string' }],
      // characters
      [/'[^\\']'/, 'string'],
      [/'/, 'string.invalid']
    ],

    comment: [
      [/[^/*]+/, 'comment'],
      [/\/\*/, 'comment', '@push'], // nested comment
      ['\\*/', 'comment', '@pop'],
      [/[/*]/, 'comment']
    ],

    string: [
      [/[^\\"]+/, 'string'],
      [/\\./, 'string.escape.invalid'],
      [/"/, { token: 'string.quote', next: '@pop' }]
    ],

    whitespace: [
      [/[ \t\r\n]+/, 'white'],
      [/\/\*/, 'comment', '@comment'],
      [/\/\/.*$/, 'comment']
    ]
  }
})
// 配置更全面的关键字、类和方法补全
monaco.languages.registerCompletionItemProvider('java', {
  provideCompletionItems: (model, position) => {
    const suggestions = [
      // --- 访问修饰符 ---
      { label: 'public', kind: monaco.languages.CompletionItemKind.Keyword, insertText: 'public ', detail: 'Access Modifier' },
      { label: 'private', kind: monaco.languages.CompletionItemKind.Keyword, insertText: 'private ', detail: 'Access Modifier' },
      { label: 'protected', kind: monaco.languages.CompletionItemKind.Keyword, insertText: 'protected ', detail: 'Access Modifier' },

      // --- 基础类型和关键字 ---
      { label: 'int', kind: monaco.languages.CompletionItemKind.Keyword, insertText: 'int ', detail: 'Primitive type' },
      { label: 'double', kind: monaco.languages.CompletionItemKind.Keyword, insertText: 'double ', detail: 'Primitive type' },
      { label: 'boolean', kind: monaco.languages.CompletionItemKind.Keyword, insertText: 'boolean ', detail: 'Primitive type' },
      { label: 'String', kind: monaco.languages.CompletionItemKind.Keyword, insertText: 'String ', detail: 'Class type' },

      // --- 控制语句 ---
      {
        label: 'if',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'if (${1:condition}) {\n\t$0\n}',
        insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'If statement'
      },
      {
        label: 'for',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'for (int ${1:i} = 0; ${1:i} < ${2:count}; ${1:i}++) {\n\t$0\n}',
        insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'For loop'
      },
      {
        label: 'while',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'while (${1:condition}) {\n\t$0\n}',
        insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'While loop'
      },
      {
        label: 'switch',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'switch (${1:expression}) {\n\tcase ${2:case}:\n\t\t$0\n\t\tbreak;\n\tdefault:\n\t\tbreak;\n}',
        insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Switch statement'
      },

      // --- 常见标准类方法 ---
      {
        label: 'System.out.println',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'System.out.println(${1:message});',
        insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Print to console'
      },
      {
        label: 'System.out.print',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'System.out.print(${1:message});',
        insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Print to console without a newline'
      },
      {
        label: 'Math.max',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'Math.max(${1:value1}, ${2:value2});',
        insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Find the maximum of two values'
      },
      {
        label: 'Math.min',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'Math.min(${1:value1}, ${2:value2});',
        insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Find the minimum of two values'
      },
      {
        label: 'Collections.sort',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'Collections.sort(${1:list});',
        insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Sort a list'
      },
      // ----array常用方法---
           // --- 数组基本操作 ---
      {
        label: 'int[]',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'int[] ${1:array} = new int[${2:size}];',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Declare and initialize an integer array'
      },
      {
        label: 'String[]',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'String[] ${1:array} = new String[${2:size}];',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Declare and initialize a String array'
      },
      {
        label: 'double[]',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'double[] ${1:array} = new double[${2:size}];',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Declare and initialize a double array'
      },
      {
        label: 'array.length',
        kind: monaco.languages.CompletionItemKind.Property,
        insertText: 'array.length',
        detail: 'Get the length of the array'
      },
      {
        label: 'Arrays.fill',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'Arrays.fill(${1:array}, ${2:value});',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Fill an array with a specific value'
      },
      {
        label: 'Arrays.sort',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'Arrays.sort(${1:array});',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Sort the array in ascending order'
      },
      {
        label: 'Arrays.binarySearch',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'Arrays.binarySearch(${1:array}, ${2:key});',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Perform binary search on a sorted array'
      },
      {
        label: 'Arrays.copyOf',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'Arrays.copyOf(${1:array}, ${2:newLength});',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Copy the array to a new array with specified length'
      },
      {
        label: 'Arrays.toString',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'Arrays.toString(${1:array});',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Convert the array to a string'
      },
      {
        label: 'Arrays.equals',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'Arrays.equals(${1:array1}, ${2:array2});',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Check if two arrays are equal'
      },
      {
        label: 'for-array',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'for (int ${1:i} = 0; ${1:i} < ${2:array}.length; ${1:i}++) {\n\t$0\n}',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Iterate over the array with a for loop'
      },
      {
        label: 'for-each',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'for (${1:Type} ${2:item} : ${3:array}) {\n\t$0\n}',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Iterate over the array with a for-each loop'
      },
      // --- ArrayList 基本操作 ---
      {
        label: 'ArrayList',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'ArrayList<${1:Type}> ${2:list} = new ArrayList<>();',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Create a new ArrayList'
      },
      {
        label: 'list.add',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'list.add(${1:element});',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Add an element to the list'
      },
      {
        label: 'list.get',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'list.get(${1:index});',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Get an element from the list'
      },
      {
        label: 'list.size',
        kind: monaco.languages.CompletionItemKind.Property,
        insertText: 'list.size()',
        detail: 'Get the size of the list'
      },
      {
        label: 'list.remove',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'list.remove(${1:indexOrElement});',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Remove an element from the list by index or value'
      },
      {
        label: 'list.contains',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'list.contains(${1:element});',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Check if the list contains an element'
      },
      {
        label: 'list.clear',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'list.clear();',
        detail: 'Clear all elements in the list'
      },
      {
        label: 'list.isEmpty',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'list.isEmpty()',
        detail: 'Check if the list is empty'
      },
      {
        label: 'list.forEach',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'list.forEach(${1:item} -> {\n\t$0\n});',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Iterate over the list using forEach'
      },
      // ----字符串常见方法--
       {
        label: 'charAt',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'charAt(${1:index})',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Returns the character at the specified index'
      },
      {
        label: 'substring',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'substring(${1:beginIndex}, ${2:endIndex})',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Returns a substring between specified indexes'
      },
      {
        label: 'indexOf',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'indexOf(${1:charOrString})',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Returns the index of the first occurrence of the specified character or string'
      },
      {
        label: 'lastIndexOf',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'lastIndexOf(${1:charOrString})',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Returns the index of the last occurrence of the specified character or string'
      },
      {
        label: 'toUpperCase',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'toUpperCase()',
        detail: 'Converts all characters in this string to uppercase'
      },
      {
        label: 'toLowerCase',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'toLowerCase()',
        detail: 'Converts all characters in this string to lowercase'
      },
      {
        label: 'trim',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'trim()',
        detail: 'Trims whitespace from both ends of the string'
      },
      {
        label: 'replace',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'replace(${1:oldChar}, ${2:newChar})',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Replaces each occurrence of oldChar with newChar'
      },
      {
        label: 'replaceAll',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'replaceAll(${1:regex}, ${2:replacement})',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Replaces each substring of this string that matches the regex with replacement'
      },
      {
        label: 'split',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'split(${1:regex})',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Splits this string around matches of the regex'
      },
      {
        label: 'contains',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'contains(${1:sequence})',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Returns true if this string contains the specified sequence'
      },
      {
        label: 'startsWith',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'startsWith(${1:prefix})',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Returns true if this string starts with the specified prefix'
      },
      {
        label: 'endsWith',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'endsWith(${1:suffix})',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Returns true if this string ends with the specified suffix'
      },
      {
        label: 'length',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'length()',
        detail: 'Returns the length of the string'
      },
      {
        label: 'isEmpty',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'isEmpty()',
        detail: 'Returns true if the string is empty'
      },
      {
        label: 'equals',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'equals(${1:object})',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Compares this string to the specified object'
      },
      {
        label: 'equalsIgnoreCase',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'equalsIgnoreCase(${1:string})',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Compares this string to another string, ignoring case considerations'
      },
      {
        label: 'format',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'String.format(${1:format}, ${2:args})',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Returns a formatted string using the specified format and arguments'
      },
      {
        label: 'valueOf',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: 'String.valueOf(${1:object})',
        insertTextRules:
        monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Returns the string representation of the specified object'
      },

      // --- 代码片段 ---
      {
        label: 'main',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'public class Main {\n\t$0\n}',
        insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Main method'
      },
      {
        label: 'class-template',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'public class ${1:ClassName} {\n\tpublic ${1:ClassName}() {\n\t\t$0\n\t}\n}',
        insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Class template'
      },
      {
        label: 'try-catch',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'try {\n\t$0\n} catch (${1:Exception e}) {\n\te.printStackTrace();\n}',
        insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Try-catch block'
      },
      {
        label: 'list-template',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'List<${1:Type}> ${2:listName} = new ArrayList<>();',
        insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'List initialization'
      },
      {
        label:'psvm',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'public class Main{\n public static void main(String[] args) {\n };}',
        insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Main method'
      },
      {
        label:'template',
        kind:monaco.languages.CompletionItemKind.Snippet,
        insertText:'public class Main{\n public static void main(String[] args) {\n };}',
        detail:'Necessary template'
      },
      {
        label: 'map-template',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'Map<${1:KeyType}, ${2:ValueType}> ${3:mapName} = new HashMap<>();',
        insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Map initialization'
      }
    ];

    return { suggestions };
  }
});
}
export default JavaWorker;

